home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Freeware / First Page 2006 3.00 / fp2006-final-3.00-setup.exe / {app} / Iscripts / Forms Misc / time-val.izs < prev    next >
Text File  |  2005-09-28  |  4KB  |  171 lines

  1. <!NOWIZARD>
  2.  
  3. <!TITLE>Time Validation 
  4. <!/TITLE>
  5.  
  6. <!DESCRIPTION> This function verifies that a string is a valid time, in the form hh:mm:ss am/pm, where seconds are optional. It accepts military time (hour between 0 and 23) as long as am/pm isn't specified. It requires am/pm when the hour is less than or equal to 12.  <!/DESCRIPTION> 
  7.  
  8. <!CATEGORY>Forms<!/CATEGORY>
  9.  
  10. <!SCRIPT>
  11. <!-- START OF SCRIPT -->
  12.  
  13. <!-- HOW TO INSTALL TIME VALIDATION:
  14.  
  15.   1.  Copy code into the HEAD section of document
  16.   2.  Put last coding into the BODY section of document  -->
  17.  
  18. <!-- STEP ONE: Add code into HEAD section of document  -->
  19.  
  20. <HEAD>
  21.  
  22. <SCRIPT LANGUAGE="JavaScript">
  23. <!-- Original:  Sandeep Tamhankar (stamhankar@hotmail.com) -->
  24. <!-- Web Site:  http://207.20.242.93 -->
  25.  
  26.  
  27.  
  28. <!-- Begin
  29. function IsValidTime(timeStr) {
  30. // Checks if time is in HH:MM:SS AM/PM format.
  31. // The seconds and AM/PM are optional.
  32.  
  33. var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
  34.  
  35. var matchArray = timeStr.match(timePat);
  36. if (matchArray == null) {
  37. alert("Time is not in a valid format.");
  38. return false;
  39. }
  40. hour = matchArray[1];
  41. minute = matchArray[2];
  42. second = matchArray[4];
  43. ampm = matchArray[6];
  44.  
  45. if (second=="") { second = null; }
  46. if (ampm=="") { ampm = null }
  47.  
  48. if (hour < 0  || hour > 23) {
  49. alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
  50. return false;
  51. }
  52. if (hour <= 12 && ampm == null) {
  53. if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
  54. alert("You must specify AM or PM.");
  55. return false;
  56.    }
  57. }
  58. if  (hour > 12 && ampm != null) {
  59. alert("You can't specify AM or PM for military time.");
  60. return false;
  61. }
  62. if (minute<0 || minute > 59) {
  63. alert ("Minute must be between 0 and 59.");
  64. return false;
  65. }
  66. if (second != null && (second < 0 || second > 59)) {
  67. alert ("Second must be between 0 and 59.");
  68. return false;
  69. }
  70. return false;
  71. }
  72. //  End -->
  73. </script>
  74. </HEAD>
  75.  
  76. <!-- STEP TWO: Add code into BODY section of document  -->
  77.  
  78. <BODY>
  79.  
  80. <center>
  81. <form name=timeform onSubmit="return IsValidTime(document.timeform.time.value);">
  82. Time (HH:MM:SS AM/PM format)  <input type=text name=time><br>
  83. <input type="submit" value="Submit">
  84. </form>
  85. </center>
  86.  
  87. <!-- END OF SCRIPT -->
  88. <!/SCRIPT>
  89.  
  90. <!PREVIEW>
  91. <!-- START OF SCRIPT -->
  92.  
  93.  
  94. <!-- HOW TO INSTALL TIME VALIDATION:
  95.  
  96.   1.  Copy code into the HEAD section of document
  97.   2.  Put last coding into the BODY section of document  -->
  98.  
  99. <!-- STEP ONE: Add code into HEAD section of document  -->
  100.  
  101. <HEAD>
  102.  
  103. <SCRIPT LANGUAGE="JavaScript">
  104. <!-- Original:  Sandeep Tamhankar (stamhankar@hotmail.com) -->
  105. <!-- Web Site:  http://207.20.242.93 -->
  106.  
  107.  
  108.  
  109. <!-- Begin
  110. function IsValidTime(timeStr) {
  111. // Checks if time is in HH:MM:SS AM/PM format.
  112. // The seconds and AM/PM are optional.
  113.  
  114. var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
  115.  
  116. var matchArray = timeStr.match(timePat);
  117. if (matchArray == null) {
  118. alert("Time is not in a valid format.");
  119. return false;
  120. }
  121. hour = matchArray[1];
  122. minute = matchArray[2];
  123. second = matchArray[4];
  124. ampm = matchArray[6];
  125.  
  126. if (second=="") { second = null; }
  127. if (ampm=="") { ampm = null }
  128.  
  129. if (hour < 0  || hour > 23) {
  130. alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
  131. return false;
  132. }
  133. if (hour <= 12 && ampm == null) {
  134. if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
  135. alert("You must specify AM or PM.");
  136. return false;
  137.    }
  138. }
  139. if  (hour > 12 && ampm != null) {
  140. alert("You can't specify AM or PM for military time.");
  141. return false;
  142. }
  143. if (minute<0 || minute > 59) {
  144. alert ("Minute must be between 0 and 59.");
  145. return false;
  146. }
  147. if (second != null && (second < 0 || second > 59)) {
  148. alert ("Second must be between 0 and 59.");
  149. return false;
  150. }
  151. return false;
  152. }
  153. //  End -->
  154. </script>
  155. </HEAD>
  156.  
  157. <!-- STEP TWO: Add code into BODY section of document  -->
  158.  
  159. <BODY>
  160.  
  161. <center>
  162. <form name=timeform onSubmit="return IsValidTime(document.timeform.time.value);">
  163. Time (HH:MM:SS AM/PM format)  <input type=text name=time><br>
  164. <input type="submit" value="Submit">
  165. </form>
  166. </center>
  167. <!-- END OF SCRIPT -->
  168. <!/PREVIEW>
  169.  
  170. <!RELATED>NONE<!/RELATED>
  171.